package com.app.utils.util;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.utils.R;


/**
 * 屏幕Toast显示工具类
 *
 * @author xuxinghai
 */
public class ToastUtils {
    private static Toast mToast;
    private static long mLastTime;
    private static String mLastStr = "";
    private static int mDrawable;
    private static boolean isUse = false;

    @SuppressLint("HandlerLeak")
    private static Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 0:
                    if (!isUse) {
                        cancelToast();
                        mToast = null;
                    } else {
                        handler.sendEmptyMessageDelayed(0, 300);
                    }
                    break;
            }
        }

        ;
    };

    /**
     * 弹出消息(两秒内连续点击不起作用)
     *
     * @param text
     */
    public static void makeText(String text) {
        isUse = true;
        if (mLastStr != null && mLastStr.equals(text)) {
            if (System.currentTimeMillis() - mLastTime < 2000) {
                return;
            }
        }
        mLastStr = text;
        mLastTime = System.currentTimeMillis();
        if (mToast == null) {
            try {
                mToast = new Toast(AppUtils.getInstance().getContext());
                mToast.setGravity(Gravity.CENTER, 0, 0);
                mToast.setDuration(Toast.LENGTH_SHORT);
                View layout = LayoutInflater.from(AppUtils.getInstance().getContext()).inflate(R.layout.toast_center_text, null);
                ((TextView) layout.findViewById(android.R.id.text1)).setText(text);
                mToast.setView(layout);
                handler.sendEmptyMessageDelayed(0, 2000);
            } catch (Exception e) {
                isUse = false;
                handler.sendEmptyMessage(0);
                return;
            }
        } else {
            if (mToast != null)
                ((TextView) (mToast.getView().findViewById(android.R.id.text1))).setText(text);
        }
        mToast.show();
        isUse = false;
    }

    /**
     * 取消toash
     */
    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = null;
    }

    /**
     * 展示文字
     *
     * @param text
     */
    public static void showText(String text) {
        Toast.makeText(AppUtils.getInstance().getContext(), text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 弹出消息(两秒内连续点击不起作用)
     *
     * @param drawable
     */
    public static void makeText(int drawable, String str) {
        isUse = true;
        if (AppUtils.getInstance().getContext() != null) {

            if (drawable == mDrawable) {
                if (System.currentTimeMillis() - mLastTime < 2000) {
                    return;
                }
            }
            mDrawable = drawable;
            mLastTime = System.currentTimeMillis();
            if (mToast == null) {
                try {
                    mToast = new Toast(AppUtils.getInstance().getContext());
                    mToast.setGravity(Gravity.CENTER, 0, 0);
                    mToast.setDuration(Toast.LENGTH_SHORT);
                    View layout = LayoutInflater.from(AppUtils.getInstance().getContext()).inflate(R.layout.toast_center_img_text, null);

                    ImageView icon = (ImageView) layout.findViewById(android.R.id.icon);
                    icon.setImageResource(drawable);
                    TextView text1 = (TextView) layout.findViewById(android.R.id.text1);
                    text1.setText(str);
                    mToast.setView(layout);
                    handler.sendEmptyMessageDelayed(0, 2000);
                } catch (Exception e) {
                    isUse = false;
                    handler.sendEmptyMessage(0);
                    return;
                }
            } else {
                if (mToast.getView() != null) {
                    ImageView icon = (ImageView) mToast.getView().findViewById(android.R.id.icon);
                    if (icon != null) {
                        icon.setImageResource(drawable);
                    }
                    TextView text = (TextView) mToast.getView().findViewById(android.R.id.text1);
                    if (text != null) {
                        text.setText(str);
                    }
                } else {
                    isUse = false;
                    return;
                }
            }
            mToast.show();
            isUse = false;
        }

    }

    /**
     * 展示自定义view的T
     *
     * @param view
     */
    public static void makeCustomView(int view) {
        Toast toast = new Toast(AppUtils.getInstance().getContext());
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        try {
            View layout = LayoutInflater.from(AppUtils.getInstance().getContext()).inflate(view, null);
            toast.setView(layout);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
